package my.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONObject;

import af.spring.AfRestData;
import af.spring.AfRestError;
import my.db.Student;
import my.tool.MybatisApi;

@Controller
public class LoginController {
	
	@GetMapping("login")
	public String hello() {
		
		return "Login";
	}
	
	@PostMapping("Studentlogin.do")
	public Object Studentlogin(@RequestBody JSONObject jreq,HttpServletRequest request) {
		
		System.out.println("学生");
		Map<String, Object> user = new HashMap<String, Object>();
		user.put("StudentId", jreq.getInteger("userID"));
		user.put("Password", jreq.getString("password"));
		List<Student>  list = MybatisApi.selectList("zhy.sesLogin.getStudent", user);
		
		if(list.size() == 0) {
			return new AfRestError("没有找到账户信息");
		}else {
			HttpSession session = request.getSession();
			session.setAttribute("student", list.get(0));
		}
		
		return new AfRestData("");
	}
	
	@PostMapping("Teacherlogin.do")
	public Object Teacherlogin(@RequestBody JSONObject jreq,HttpServletRequest request) {
		System.out.println("教师");
		Map<String, Object> user = new HashMap<String, Object>();
		user.put("TeacherId", jreq.getInteger("userID"));
		user.put("Password", jreq.getString("password"));
		List<Student>  list = MybatisApi.selectList("zhy.sesLogin.getTeacher", user);
		
		if(list.size() == 0) {
			return new AfRestError("没有找到账户信息");
		}else {
			HttpSession session = request.getSession();
			session.setAttribute("teacher", list.get(0));
		}
		return new AfRestData("");
	}
	
	@PostMapping("loginOut.do")
	public Object LoginOut(@RequestBody JSONObject jreq,HttpSession session)
	{
		session.removeAttribute(jreq.getString("user"));
		return new AfRestData("");
	}
}
